


//
//  HMHttpTool.m
//  黑马微博-wzy
//
//  Created by Mac on 2017/2/15.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "HMHttpTool.h"

@implementation HMHttpTool

/*
//success block 方法
success = ^(id responseObject){
    //成功
    HMUser *user = [HMUser objectWithKeyValues:responseObject];
    //设置用户昵称为nav 标题
    [self.titleButton setTitle:user.name forState:UIControlStateNormal];
    //存储账号信息
    HMAccount *account = [HMAccountTool account];
    account.name = user.name;
    [HMAccountTool save:account];
}
*/

/*
failure = ^(NSError *error){
    [MBProgressHUD showError:@"获取数据失败!"];
    HMLog(@"%@", error);
 }
 */

//GET  请求
+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void(^)(id responseObj))success failure:(void(^)(NSError *error))failure{
     //1.获取请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //2.发送get 请求
    [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        HMLog(@"首页:%@", responseObject);
        
        if (success) {
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //失败
        if(failure){
            failure(error);
        }
        
    }];

}


//POST  请求
+ (void)post:(NSString *)url params:(NSDictionary *)params success:(void(^)(id responseObj))success failure:(void(^)(NSError *error))failure{
    //1.获取请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //2.发送get 请求
    [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //成功
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //失败
        if(failure){
            failure(error);
        }

    }];
    
}

@end


////复习block
//void test(){
//    
//    //例如1.
//    void (^block)() = ^{
//    
//    };
//    
//    //例如2.
//    int (^sum)(int, int) = ^(int a, int b){
//        return a + b;
//    };
//    //"=" 左边 block 的声明:返回值类型(^ block名称)(参数列表)
//    //"=" 右边 block 的定义:^(参数列表)
//    
//    //3.block 类型,就是int (^)(int, int)
//}

